
#include <stdio.h>
#include <ctype.h>
#include <assert.h>

#include "ngram.h"

/* getword
 * read a word from fp and store it as \0-terminated string in buf.
 * Returns 1 on success, and 0 when the EOF is reached. A word is a
 * sequence of isalpha() characters. Other characters are skipped.
 */
    
static int getword(FILE *fp, char *buf, int size) {
    int i = 0, c;
    assert(fp && buf && size > 1);
    
    c = getc(fp);
    for (; c != EOF; c = getc(fp)) {
        if (isalpha(c)) {
            if (i < size - 1) {
                buf[i++] = c;
                c = getc(fp);
                break;
            }      
        }
    }    
    for (; c != EOF && (isalpha(c) || c == '_'); c = getc(fp)) 
        if (i < size - 1)
            buf[i++] = c;
    
    if (i < size)
        buf[i] = '\0';
    else
        buf[size-1] = '\0';
    if (c != EOF)
        ungetc(c, fp);
        
    return i > 0;
}
 
/* main
 */ 
   
int main(int argc, char *argv[])
{
    char buf[1024];

    while(getword(stdin, buf, sizeof buf)) {
        ngram_string(buf);
    }     
    return 0;
}
